(ns main
  (load-file "src/rete.clj")
  (:require [clojure.xml :as xml])

  (:import (javax.swing JFrame JTextField JButton JPanel JLabel JTextArea JScrollPane BoxLayout BorderFactory)
           (java.awt.event ActionListener)
           (java.awt GridLayout BorderLayout FlowLayout Dimension Color))
  )
  ;::::::: MAIN ::::::::::
(def xml-doc (xml/parse "src/reglas.xml"))
(rete/createRete xml-doc)

(def wk (ref #{}))

(defn addAction
  "Creates a new button and adds the works too it."
  [button textField label]
  (. button addActionListener
      (proxy [ActionListener] []
        (actionPerformed [e]
          (dosync
            (if (re-find #"^[^=]*=[^=]*$" (. textField getText))
              (let [nodo (common.Node.)
                    rule (. (. textField getText) split "=")]      
                
                (rete/loadNode nodo "input" (first rule) (second rule))            
                (rete/addFact nodo)
                
                (. textField setText "")            
                (def texto "")
            (doseq [x (. (. @rete/workingMemory toString) split "#")]
              
                (def texto (str texto "\n" x))                
                              
              (. label setText (. texto replaceAll "Node" ""))
              
          )
          )          
          )
        )
        )
      )
  )
  )
  




(let [frame (JFrame. "Rete")
        okButton (JButton. "OK")
        cleanButton (JButton. "Borrar")
        imprimirButton (JButton. "Ver Red")
        text-field (JTextField. "d=1" 20)
        outter-pane (JPanel. (BorderLayout. 0 1))
        southPane (JPanel. (FlowLayout.))
        northPane (JPanel. (BorderLayout. 1 0))
        centerPane (JScrollPane.)
        labelRegla (JLabel. "Regla: ")
        areaWK (JTextArea. "")]  
  (addAction okButton text-field areaWK)
  (. cleanButton addActionListener
      (proxy [ActionListener] []
        (actionPerformed [e]
          (dosync 
            (ref-set rete/reteNet (hash-map))
            (ref-set rete/workingMemory #{})
            )
          (rete/createRete xml-doc)
          (. areaWK setText "")
        )))
  
  (. imprimirButton addActionListener
      (proxy [ActionListener] []
        (actionPerformed [e]
          (println @rete/reteNet)
        )))
  (doto areaWK
    (.setEditable false)
    )
  (doto labelRegla
    (.setSize 40 40)
    )
  (doto southPane
    (.add cleanButton)
    (.add imprimirButton)
    (.add okButton)
    (.setBorder (. BorderFactory createLineBorder (Color/black)))      
    )
  (doto northPane
    (.add labelRegla BorderLayout/CENTER)
    (.add text-field BorderLayout/EAST)
    (.setBorder (. BorderFactory createLineBorder (Color/black)))
    )
  (doto centerPane      
    (.setViewportView areaWK)    
    (.setBorder (. BorderFactory createLineBorder (Color/black)))      
    )
  (doto outter-pane
    (.add northPane BorderLayout/NORTH)
    (.add centerPane BorderLayout/CENTER)
    (.add southPane BorderLayout/SOUTH)
    )
  (doto frame
    (.add outter-pane)
    (.setDefaultCloseOperation JFrame/EXIT_ON_CLOSE)
    (.setResizable false)
    (.setSize 400 300)
    (.setVisible true)
    )
  )


(let [n (common.AlphaNode.)]
  (rete/loadNode n "s" "c" "1")
  ;(rete/addFact n)
  )
